package com.codingke.codingbookstore.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.codingke.codingbookstore.App;
import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.utils.Constant;
import com.codingke.codingbookstore.utils.NumberUtils;
import com.codingke.codingbookstore.vo.Comment;
import com.codingke.codingbookstore.vo.Orders;
import com.codingke.codingbookstore.vo.User;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

public class EvaluationActivity extends BaseActivity implements View.OnClickListener{

    private Orders orders;
    private ImageView imageView3_bookimage;
    private TextView textView7_bookName;
    private TextView textView10_discount_price;
    private RatingBar ratingBar_star;
    private EditText editText_evaluation;
    private Button button_submit;
    private ImageView imageView_shopping_cart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluation);
        imageView3_bookimage = (ImageView) findViewById(R.id.imageView3_bookimage);
        textView7_bookName = (TextView) findViewById(R.id.textView7_bookName);
        textView10_discount_price = (TextView) findViewById(R.id.textView10_discount_price);
        ratingBar_star = (RatingBar) findViewById(R.id.ratingBar_star);
        editText_evaluation = (EditText) findViewById(R.id.editText_evaluation);
        button_submit = (Button) findViewById(R.id.button_submit);
        button_submit.setOnClickListener(this);
        imageView_shopping_cart = (ImageView) findViewById(R.id.imageView_shopping_cart);
        imageView_shopping_cart.setOnClickListener(this);
        initData();

    }

    private void initData() {
        orders = (Orders) getIntent().getSerializableExtra("orders");
        textView7_bookName.setText(orders.getBookName());
        textView10_discount_price.setText("价格:"+ NumberUtils.format(orders.getDiscountPrice()));
        App.bitmapUtils.display(imageView3_bookimage, Constant.BMOBFILE_ROOT+orders.getBookImage());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_submit:
                submitEvaluation();
                break;
            case R.id.imageView_shopping_cart:
                startActivity(new Intent(this,ShoppingCartActivity.class));
                finish();
                break;
            default:
                break;
        }
    }

    private void submitEvaluation() {
        String content = editText_evaluation.getText().toString();
        if(TextUtils.isEmpty(content)){
            showToast("请输入商品评价");
            return;
        }
        int star = (int) ratingBar_star.getRating();

        Comment comment = new Comment();
        User user = BmobUser.getCurrentUser(this, User.class);
        comment.setBookInfoId(orders.getBookInfoId());
        comment.setStar(star);
        comment.setContent(content);
        comment.setUser(user.getUsername());
        comment.setUserId(user.getObjectId());

        comment.save(this, new SaveListener() {
            @Override
            public void onSuccess() {
                showToast("评价发表成功");
                orders.setStatus(Constant.ORDER_HAS_BEEN_EVALUATED);
                orders.update(EvaluationActivity.this);
                finish();
            }

            @Override
            public void onFailure(int i, String s) {
                showToast("评价失败");
            }
        });

    }

}
